export const elements = [
  {
    title: '数据集',
    id: 1,
    isSubshow: false,
    children: [
      { title: '表输入', id: 101, imgURL: 'shuru.png' },
      { title: '表输出', id: 101, imgURL: 'shuchu.png' }
    ]
  },
  {
    title: '列转换',
    id: 2,
    isSubshow: false,
    children: [
      { title: '字段选择', id: 200, imgURL: 'ziduanxz.png' },
      { title: '列拆分为多行', id: 200, imgURL: 'lie-hang.png' },
      { title: '列转行', id: 200, imgURL: 'lieToHang.png' },
      { title: '增加序列', id: 200, imgURL: 'addlie.png' }
    ]
  },
  {
    title: '行转换',
    id: 3,
    isSubshow: false,
    children: [
      { title: '行转列', id: 301, imgURL: 'hangToLie.png' },
      { title: '过滤记录', id: 301, imgURL: 'guolv.png' },
      { title: '去除重复记录', id: 301, imgURL: 'quchong.png' },
      { title: '排序记录', id: 301, imgURL: 'sort.png' },
      { title: '分组', id: 301, imgURL: 'fenzu.png' },
      { title: '记录集连续', id: 301, imgURL: 'jilvji.png' },
      { title: '追加流', id: 301, imgURL: 'zhuijialiu.png' }
    ]
  },
  {
    title: '扩展',
    id: 4,
    isSubshow: false,
    children: [
      { title: '生成随机数', id: 301, imgURL: 'suijishu.png' },
      { title: '自定义常量数据', id: 301, imgURL: 'changliang.png' },
      { title: '调用DB存储过程', id: 301, imgURL: 'cunchu.png' },
      { title: 'JavaScript', id: 301, imgURL: 'JS.png' }
    ]
  }
]

export const arry = [
  {
    china: 'tableIn',
    text: '<Step connection="" copies="1" ctype="TableInput" distribute="Y" draw="Y" label="表输入" lazy_conversion_active="N" limit="0" lookup="" partitioning="{&quot;method&quot;:&quot;none&quot;}" sql="" supports_error_handing="N" variables_active="N"></Step>'

  },
  {
    china: 'selectValues',
    text: '<Step copies="1" ctype="SelectValues" distribute="Y" draw="Y" fields="[]" label="字段选择" meta="[]" partitioning="{&quot;method&quot;:&quot;none&quot;}" remove="[]" select_unspecified="N" supports_error_handing="Y"></Step>'
  },
  {
    china: 'tableOut',
    text: '<Step commit="1000" connection="" copies="1" ctype="TableOutput" distribute="Y" draw="Y" fields="[]" ignore_errors="N" label="表输出" partitioning="{&quot;method&quot;:&quot;none&quot;}" partitioning_daily="N" partitioning_enabled="N" partitioning_field="" partitioning_monthly="Y" return_field="" return_keys="N" schema="" specify_fields="N" supports_error_handing="Y" table="" tablename_field="" tablename_in_field="N" tablename_in_table="Y" truncate="N" use_batch="Y"></Step>'
  },
  // 去重
  {
    china: 'unique',
    text: '<Step copies="1" count_field="" count_rows="N" ctype="Unique" distribute="Y" draw="Y" error_description="" fields="" label="去除重复记录" partitioning="{&quot;method&quot;:&quot;none&quot;}" reject_duplicate_row="N" supports_error_handing="N" id="">'
  },
  // 排序
  {
    china: 'sortRows',
    text: '<Step compress="N" compress_variable="" copies="1" ctype="SortRows" directory="%%java.io.tmpdir%%" distribute="Y" draw="Y"  free_memory="" label="排序记录" partitioning="{&quot;method&quot;:&quot;none&quot;}" prefix="out" sort_size="1000000" supports_error_handing="N" unique_rows="N" id="">'
  },
  // 列拆分为多行
  {
    china: 'splitFieldToRows',
    text: '<Step  ctype="SplitFieldToRows3" draw="Y"  label="列拆分为多行" partitioning="{&quot;method&quot;:&quot;none&quot;}" id=""></Step>'
  },
  // 记录集连接
  {
    china: 'mergeJoin',
    text: '<Step copies="1" ctype="MergeJoin" distribute="Y" draw="Y" join_type="" key1="" key2="" label="记录集连接"  partitioning="{&quot;method&quot;:&quot;none&quot;}" step1="" step2="" supports_error_handing="N" id="">'
  },
  // 过滤
  {
    china: 'filterRows',
    text: '<Step condition="{&quot;negated&quot;:false,&quot;operator&quot;:0,&quot;func&quot;:0}" copies="1" ctype="FilterRows" distribute="Y" draw="Y" label="过滤记录" partitioning="{&quot;method&quot;:&quot;none&quot;}" send_false_to="" send_true_to="" supports_error_handing="N" id="">'
  },
  // 模拟数据
  // 追加流
  {
    china: 'Append',
    text: '<Step copies="1" ctype="Append" distribute="Y" draw="Y" head_name="" label="追加流" partitioning="{&quot;method&quot;:&quot;none&quot;}" supports_error_handing="N" tail_name="" id="">'
  },
  // 分组
  {
    china: 'aaa3',
    text: '<Step commit="1000" connection="" copies="1" ctype="SplitFieldToRows" distribute="Y" draw="Y" fields="[]" ignore_errors="N" label="表输出" partitioning="{&quot;method&quot;:&quot;none&quot;}" partitioning_daily="N" partitioning_enabled="N" partitioning_field="" partitioning_monthly="Y" return_field="" return_keys="N" schema="" specify_fields="N" supports_error_handing="Y" table="" tablename_field="" tablename_in_field="N" tablename_in_table="Y" truncate="N" use_batch="Y"></Step>'
  },
  {
    china: 'lieTo',
    text: '<Step commit="1000" connection="" copies="1" ctype="SplitFieldToRows" distribute="Y" draw="Y" fields="[]" ignore_errors="N" label="表输出" partitioning="{&quot;method&quot;:&quot;none&quot;}" partitioning_daily="N" partitioning_enabled="N" partitioning_field="" partitioning_monthly="Y" return_field="" return_keys="N" schema="" specify_fields="N" supports_error_handing="Y" table="" tablename_field="" tablename_in_field="N" tablename_in_table="Y" truncate="N" use_batch="Y"></Step>'
  },
  {
    china: 'changliang',
    text: '<Step commit="1000" connection="" copies="1" ctype="SplitFieldToRows" distribute="Y" draw="Y" fields="[]" ignore_errors="N" label="表输出" partitioning="{&quot;method&quot;:&quot;none&quot;}" partitioning_daily="N" partitioning_enabled="N" partitioning_field="" partitioning_monthly="Y" return_field="" return_keys="N" schema="" specify_fields="N" supports_error_handing="Y" table="" tablename_field="" tablename_in_field="N" tablename_in_table="Y" truncate="N" use_batch="Y"></Step>'
  }
]

export const trashop = '<TransHop enable="Y" id=""></TransHop>'

// 获取子元素属性的方法
export const getChildAttributes = /* TODO */(el, attr) => {
  const children = Array.from(el.childNodes).filter((child) => child.nodeType === 1)
  return children.map((child) => {
    const attrs = Array.from(child.attributes)
    let pos

    return attrs.find((value, index) => {
      pos = index
      return value.name === attr
    }) !== undefined ? attrs[pos].value : null
  })
}

// 字符串xml格式化
export const xmlFormalAll = (xml) => {
  // 格式化
  function formatXml(text) {
    // 去掉多余的空格
    text = '\n' + text.replace(/(<\w+)(\s.*?>)/g,
      function($0, name, props) {
        return name + ' ' + props.replace(/\s+(\w+=)/g, ' $1')
      }).replace(/>\s*?</g, '>\n<')

    // 把注释编码
    text = text.replace(/\n/g, '\r').replace(/<!--(.+?)-->/g,
      function($0, text) {
        var ret = '<!--' + escape(text) + '-->'
        return ret
      }).replace(/\r/g, '\n')

    // 调整格式
    var rgx = /\n(<(([^\?]).+?)(?:\s|\s*?>|\s*?(\/)>)(?:.*?(?:(?:(\/)>)|(?:<(\/)\2>)))?)/mg
    var nodeStack = []
    var output = text.replace(rgx, function($0, all, name, isBegin, isCloseFull1, isCloseFull2, isFull1, isFull2) {
      var isClosed = (isCloseFull1 === '/') || (isCloseFull2 === '/') || (isFull1 === '/') || (isFull2 === '/')
      var prefix = ''
      if (isBegin === '!') {
        prefix = getPrefix(nodeStack.length)
      } else {
        if (isBegin !== '/') {
          prefix = getPrefix(nodeStack.length)
          if (!isClosed) {
            nodeStack.push(name)
          }
        } else {
          nodeStack.pop()
          prefix = getPrefix(nodeStack.length)
        }
      }
      var ret = '\n' + prefix + all
      return ret
    })

    // var prefixSpace = -1
    var outputText = output.substring(1)

    // 把注释还原并解码，调格式
    outputText = outputText.replace(/\n/g, '\r').replace(/(\s*)<!--(.+?)-->/g,
      function($0, prefix, text) {
        if (prefix.charAt(0) === '\r') prefix = prefix.substring(1)
        text = unescape(text).replace(/\r/g, '\n')
        var ret = '\n' + prefix + '<!--' + text.replace(/^\s*/mg, prefix) + '-->'
        return ret
      })
    return outputText.replace(/\s+$/g, '').replace(/\r/g, '\r\n')
  }

  function getPrefix(prefixIndex) {
    var span = '    '
    var output = []
    for (var i = 0; i < prefixIndex; ++i) {
      output.push(span)
    }
    console.log(output.join(''))
    return output.join('')
  }
  xml = formatXml(xml)
  return xml
}
// 字符串转层dom元素
export const parseDom = (arg) => {
  var objE = document.createElement('step')
  objE.innerHTML = arg
  return objE.childNodes
}

// xml转字符串
export const xmlToString = (xmlObj) => {
  if (document.all) {
    return xmlObj.xml
  } else {
    return (new XMLSerializer()).serializeToString(xmlObj)
  }
}
